<?php
/**
 * Administracja - lista studentów
 * @author Miron Pawlik
 */


//sprawdzanie czy formularz zostal wyslany
if($fd['editData'])
{
	$errorMsg = "";
	foreach($fd['indexID'] as $key => $indexID)
	{
		$errorMsgLocal = "";
		if(strlen($fd['name'][$key]) < 2)
			$errorMsgLocal .= "Podaj imię ($indexID)<br/>";
		if(strlen($fd['surname'][$key]) < 2)
			$errorMsgLocal .= "Podaj nazwisko ($indexID)<br/>";
		if($fd['year'][$key] < 2003 || $fd['year'][$key] > date('Y'))
			$errorMsgLocal .= "Podaj rok ($indexID)<br/>";
		
		
		if(!$errorMsgLocal)
		{
			$user->updateUser($db, $fd['indexID'][$key], $fd['name'][$key], $fd['surname'][$key], $fd['year'][$key], $fd['password'][$key], $fd['locked'][$key], $fd['admin'][$key]);
		}
		else
			$errorMsg .= $errorMsgLocal;
	}
	if(!$errorMsg)
		header("Location: ".$_conf['host'].'?admin,students');
}
if($fd['type'] == 'add')
{
	//walidacja danych
	if(strlen($fd['name']) < 2)
		$errorMsg = "Podaj imię";
	if(strlen($fd['surname']) < 2)
		$errorMsg = "Podaj nazwisko";
	if($fd['year'] < 2003 || $fd['year'] > date('Y'))
		$errorMsg = "Podaj rok";
	if(strlen($fd['password']) < 2)
		$errorMsg = "Podaj hasło";
	
	//edytowanie danych studenta
	//tworzenie nowego studenta
	if(!$errorMsg)
	{
		$user->addUser($db, $fd['indexID'], $fd['name'], $fd['surname'], $fd['year'], $fd['password'], $fd['admin']);
		header("Location: ".$_conf['host'].'?admin,students');
	}
}


//usuwanie studenta
if($fd[2] == 'del')
{
	//sprawdzamy czy admin nie usuwa samego siebie
	if($fd[3] != $user->getId())
	{
		$db->delById($fd[3], 'students', 'indexID');
		$db->delById($fd[3], 'studentTerms', 'indexID');
		$db->delById($fd[3], 'chosenTerms', 'indexID');
		$db->delById($fd[3], 'studentSubjects', 'indexID');
		$db->delById($fd[3], 'groups', 'ownerID');
		$db->delById($fd[3], 'groups', 'memberID');
		header("Location: ".$_conf['host'].'?admin,students');
	}
	else
		$errorMsg = "Nie można usunąć samego siebie !";
}

$all = $db->getAll('students', 'surname');


$smarty->assign('students', $all);
?>
